//
//  MyScrollView.m
//  封装滚动视图
//
//  Created by rimi on 16/8/26.
//  Copyright © 2016年 rimi. All rights reserved.
//

#import "MyScrollView.h"
@interface MyScrollView()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation MyScrollView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
     
        
    }
    return self;
}
- (void)addImageWithScrollV {
    // 循环添加图片
    for (NSInteger index = 0; index < self.imageNames.count; index ++) {
        // 1.初始化图片视图
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * index, 0, self.scrollView.frame.size.width,self.scrollView.frame.size.height)];
        imageView.image = [UIImage imageNamed:self.imageNames[index]];
        // 添加当前图片视图到滚动视图
        [self.scrollView addSubview:imageView];
    }
    // 图片添加完成后设置滚动范围
    self.scrollView.contentSize = CGSizeMake(self.imageNames.count * self.scrollView.frame.size.width, 0);
}
#pragma mark - 定时器方法
- (void)startTimer {
    // 初始化定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
}

- (void)stopTimer {
    // 退出定时器
    [_timer invalidate];
    // 值空
    _timer = nil;
}
// 定时器的回调方法
- (void)scrollImage {

    // 1.获得当前的偏移量
    CGFloat offsetX = self.scrollView.contentOffset.x;
    // 2.在当前偏移量上加一张图片的宽度后重新设置偏移
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width + offsetX, 0) animated:YES];
}

#pragma mark - 滚动视图的代理方法
// 这个代理方法在滚动时会不断调用，频率很高，一般不建议使用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //    NSLog(@"%lf",scrollView.contentOffset.x);
}
// 滚动减速完成
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.scrollView) {
        
   
    // 在这个方法中获得图片滚动结束时的偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    if (offsetX > self.scrollView.frame.size.width * (self.imageNames.count-2)) {
        [scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0) animated:NO];
    }
    if (offsetX < self.scrollView.frame.size.width) {
        [scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * (self.imageNames.count-2), 0) animated:NO];
    }
         }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
     if (scrollView == self.scrollView) {
         [self scrollViewDidEndDecelerating:scrollView];}
}
// 拖动开始
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
     if (scrollView == self.scrollView) {
    // 停止定时器
         [self stopTimer];}
}
// 拖动结束
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
     if (scrollView == self.scrollView) {
    // 开启定时器
         [self startTimer];}
}

#pragma mark - 懒加载 getter和setter
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,self.frame.size.width, self.frame.size.height);
        NSLog(@"%@",NSStringFromCGRect(self.frame));
        _scrollView.backgroundColor =[UIColor orangeColor];
      ;
        // 设置分页效果
        _scrollView.pagingEnabled = YES;
        // 取消滚动条
        _scrollView.showsHorizontalScrollIndicator = NO;
        // 设置滚动视图的代理
        _scrollView.delegate = self;
        // 设置默认偏移一张图片
        _scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0);
        // 关闭弹簧效果
        _scrollView.bounces = NO;
        
    }
    return _scrollView;
}
-(void)setImageNames:(NSArray *)imageNames{
    _imageNames = imageNames;
    [self addImageWithScrollV];
    [self addSubview:self.scrollView];
    [self startTimer];
}

@end
